package cn.edu.scau.cmi.dengshixin.client;

import java.util.ArrayList;
import java.util.Scanner;
import cn.edu.scau.cmi.dengshixin.composite.safe.*;

public class SafeCompositeClient {

	private static Scanner in;

	public static void main(String[] args) {
		String name1 = "zhangsan";
		String name2 = "lisi";
		String name3 = "wangwu";
		String name4 = "zhaoliu";
		String dept_name1 = "IT";
		String dept_name2 = "Operation";
		String dept_name3 = "Development";
		
		OrganizationSafeComposite dept1 = new OrganizationSafeComposite(dept_name1);
		OrganizationSafeComposite dept2 = new OrganizationSafeComposite(dept_name2 , dept1.getName());
		OrganizationSafeComposite dept3 = new OrganizationSafeComposite(dept_name3, dept1.getName());
		
		SafePerson p1 = new SafePerson(name1, dept2.getName());
		SafePerson p2 = new SafePerson(name2, dept2.getName());
		SafePerson p3 = new SafePerson(name3, dept3.getName());
		SafePerson p4 = new SafePerson(name4, dept3.getName());

		dept1.addChild(dept2);
		dept1.addChild(dept3);
		dept2.addChild(p1);
		dept2.addChild(p2);
		dept3.addChild(p3);
		dept3.addChild(p4);
		
		ArrayList<OrganizationSafeComponent> departments = new ArrayList<OrganizationSafeComponent>();
		ArrayList<OrganizationSafeComponent> persons = new ArrayList<OrganizationSafeComponent>();
		departments.add(dept1);
		departments.add(dept2);
		departments.add(dept3);
		
		in = new Scanner(System.in);
		while(true) {
			System.out.println("\n请录入新员工姓名(回车结束)：");
			while(true) {
				String name = in.nextLine();
				if(name.equals("")) break;
				SafePerson person = new SafePerson(name);
				persons.add(person);
			}
			if(persons.isEmpty()) continue;
			
			OrganizationSafeComponent dept = null;
			System.out.println("对以上新员工，请选择以下功能：1. 录入已有部门    2.创建新部门");
			while(true) {
				int choice = in.nextInt();
				in.nextLine();
				if(choice == 1) {
					if(departments.isEmpty()) {
						System.out.println("当前没有任何部门！");
						continue;
					}
					System.out.println("当前有以下部门：");
					displayDepartments(departments);
					System.out.println("请选择：");
					while(true) {
						choice = in.nextInt();
						in.nextLine();
						if(choice > 0 && choice <= departments.size()) break;
					}
					dept = departments.get(choice-1);
				}	// choice == 1
				else if(choice == 2) {
					System.out.println("请输入新部门名字：");
					String dept_name = in.nextLine();
					System.out.println("请选择新部门隶属于那个部门：");
					System.out.println("0.null");
					displayDepartments(departments);
					while(true) {
						choice = in.nextInt();
						in.nextLine();
						if(choice >= 0 && choice <= departments.size()) break;
					}
					if(choice == 0) {	// 没有隶属于任何部门
						dept = new OrganizationSafeComposite(dept_name, null);
					}
					else {				// 隶属于某个部门
						dept = new OrganizationSafeComposite(dept_name, departments.get(choice-1).getName());
						departments.get(choice-1).addChild(dept);
					}
					departments.add(dept);
				}	// choice = 2
				else {
					continue;
				}
				
				for(OrganizationSafeComponent p : persons) {
					p.setOrganization(dept.getName());
					dept.addChild(p);
				}
				persons.clear();
				
				System.out.println("录入成功！\n");
				displayTree(departments);
				break;
			}
		}
	} // main
	
	static void displayDepartments(ArrayList<OrganizationSafeComponent> departments) {
		int i = 1;
		for(OrganizationSafeComponent dept : departments)	{
			System.out.println( i + ".[" + dept.getOrganization() + "]" + dept.getName());
			++i;
		}
	}
	
	static void displayTree(ArrayList<OrganizationSafeComponent> departments) {
		for(OrganizationSafeComponent dept : departments)	{
			if(dept.getOrganization() == null) {
				dept.doSomething();
				
			}
		}
	}

}
